package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.MajorMapper;
import com.think.teaching.model.Grade;
import com.think.teaching.model.Major;
import com.think.teaching.service.MajorService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 专业 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class MajorServiceImpl extends BaseServiceImpl<MajorMapper, Major> implements MajorService {

	@Override
	public Major AddNewMajor(Major major) {
		if (this.save(major)) {
			return major;
		}
		return null;
	}


	@Override
	public Major UpdateMajor(Major major) {
		if (this.updateById(major)) {
			return major;
		}
		return null;
	}

	@Override
	public Major SetMajorState(Integer id, Integer state) {
		UpdateWrapper<Major> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	@Override
	public Major GetMajorInfo(Integer id) {
		QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public List<Grade> GetMajorGradeList(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	public IPage<Major> ListMajorWithCollege(Integer count, Integer page, Integer college,Integer school, Boolean withoutBan) {
		
		IPage<Major> ipage = new Page<>(page, count);
		if (college>0) {
			return this.baseMapper.getMajorList(ipage,college,-1,withoutBan?1:0);

		}else if (school>0) {
			return this.baseMapper.getMajorList(ipage,-1,school,withoutBan?1:0);
		}
		return this.baseMapper.getMajorList(ipage,college,school,withoutBan?1:0);
	}

	@Override
	public List<Major> SearchMajorWithKey(String key, Boolean withoutBan) {
		QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("major_name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		queryWrapper.last("limit 0 , 10");
		return list(queryWrapper);
	}
	
	public List<com.think.teaching.model.Class> GetMajorClasses(Integer id) {
		return this.baseMapper.getMajorClasses(id);
	}
	
	public List<com.think.teaching.model.StudentInfo> GetMajorStudents(Integer id) {
		return this.baseMapper.getMajorStudents(id);
	}

}
